package org.i_plus.model.dao;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.i_plus.model.MapBean;
import org.i_plus.model.Map_ReplyBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Map_ReplyDAOHibernate {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void select() {
		Session ses = this.sessionFactory.getCurrentSession();
		Query query = ses.createQuery("from Map_ReplyBean");
		List<Map_ReplyBean> list = query.list();
		for (Map_ReplyBean bean : list) {
			System.out.print(bean.getM_re_num());
			System.out.print(bean.getMapBean());
			System.out.print(bean.getM_re_acc());
			System.out.print(bean.getM_re_content());
			System.out.println(bean.getM_re_time());
		}

	}

	public void select_By_Num(int num) {
		Session ses = this.sessionFactory.getCurrentSession();
		Map_ReplyBean bean = (Map_ReplyBean) ses.get(Map_ReplyBean.class, num);
		System.out.print(bean.getM_re_num());
		System.out.print(bean.getMapBean().getM_num());
		System.out.print(bean.getM_re_acc());
		System.out.print(bean.getM_re_content());
		System.out.println(bean.getM_re_time());
		// Map_ReplyDAOHibernate dao = new Map_ReplyDAOHibernate();
		// Map_ReplyBean temp = dao.select_By_Num(bean.getMapBean().getM_num());
		// bean.setMapBean(temp); // service 要把回覆的資料抓出來 ，需要這一段程式 。 selectByPK 用
		// ，因為文章才需要顯示回覆。而顯示文章不用
	}

	public void delete(int num) {
		Session ses = this.sessionFactory.getCurrentSession();
		Map_ReplyBean bean = (Map_ReplyBean) ses.get(Map_ReplyBean.class, num);
		ses.delete(bean);
		// ses.beginTransaction().commit();
	}

	public void insert(int num, int num2, String acc, String cont,
			java.sql.Date time) {
		Session ses = this.sessionFactory.getCurrentSession();
		Map_ReplyBean bean = new Map_ReplyBean();
		bean.setM_re_num(num);
		MapBean temp = new MapBean();
		temp.setM_num(num2);
		bean.setMapBean(temp);// 原本是 setM_num
		bean.setM_re_acc(acc);
		bean.setM_re_content(cont);
		bean.setM_re_time(time);
		ses.save(bean);
	}

	public void update(int num, int num2, String acc, String cont,
			java.sql.Date time) {
		Session ses = this.sessionFactory.getCurrentSession();
		Map_ReplyBean bean = new Map_ReplyBean();
		bean.setM_re_num(num);
		MapBean temp = new MapBean();
		temp.setM_num(num2);
		bean.setMapBean(temp);// 原本是 setM_num
		bean.setM_re_acc(acc);
		bean.setM_re_content(cont);
		bean.setM_re_time(time);
		ses.saveOrUpdate(bean);
	}
	
	public Set<Map_ReplyBean> selectMany(int M_num){
		Session session = this.sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Map_ReplyBean as bean where bean.MapBean.M_num=?");
		query.setInteger(0, M_num);
		return new HashSet<Map_ReplyBean>(query.list());
	}

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"beans.config.xml");
		Map_ReplyDAOHibernate mh = (Map_ReplyDAOHibernate) context
				.getBean("Map_ReplyDAO");
		mh.sessionFactory.getCurrentSession().beginTransaction();
		// mh.select_By_Num(12);
		// SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
		// mh.insert(4, 9,"BB", "BB", java.sql.Date.valueOf("1985-5-25"));
		// mh.select();
		// mh.delete(5);
		Set<Map_ReplyBean> set = mh.selectMany(9);
		for(Map_ReplyBean temp:set){
			System.out.println(temp.getM_re_num());
		}
		mh.sessionFactory.getCurrentSession().getTransaction().commit();
	}
	

}
